package xyz.xuminghai.javafx_learn.animation;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * 2023/7/21 13:33 星期五<br/>
 * 时间线动画
 *
 * @author xuMingHai
 */
public class TimelineApp extends Application {

    private final BorderPane root = new BorderPane();

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        Rectangle rectangle = new Rectangle(100.0, 100.0);

        Timeline timeline = new Timeline(
                new KeyFrame(Duration.millis(500.0),
                        new KeyValue(rectangle.rotateProperty(), 360.0)
                )
        );

        timeline.setOnFinished(event -> {
            // 恢复初始值
            rectangle.setRotate(0.0);
        });

        rectangle.setOnMouseEntered(event -> timeline.play());

        root.setCenter(rectangle);
    }


    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
